package com.github.postsanf.yinian.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.LookSendWebActivity;
import com.github.postsanf.yinian.activity.ShopCarListActivity;
import com.github.postsanf.yinian.activity.ShopCarOrderActivity;
import com.github.postsanf.yinian.activity.ShopContactServiceActivity;
import com.github.postsanf.yinian.activity.TimeGoodsDrawActivity;
import com.github.postsanf.yinian.activity.TimeGoodsJointActivity;
import com.github.postsanf.yinian.activity.TimeGoodsLomoPreViewActivity;
import com.github.postsanf.yinian.activity.VoiceCardPreViewActivity;
import com.github.postsanf.yinian.bean.YNOrder;
import com.github.postsanf.yinian.bean.YNShopCar;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/12/2.
 */

public class OrderListItemAdapter extends BaseExpandableListAdapter {

    private List<YNOrder> ynOrderList;
    private Context context;
    private LayoutInflater mInflater;
    private int userid;
    private boolean show = false;

    public OrderListItemAdapter(Context context, List<YNOrder> ynOrderList, int userid) {
        mInflater = LayoutInflater.from(context);
        this.ynOrderList = ynOrderList;
        this.context = context;
        this.userid = userid;
    }

    @Override
    public int getGroupCount() {
        return ynOrderList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return ynOrderList.get(groupPosition).getItem().size() - 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return ynOrderList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return ynOrderList.get(groupPosition).getItem().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.yn_item_order, parent, false);
            viewHolder.order_item_show = ((ImageView) convertView.findViewById(R.id.order_item_show));
            viewHolder.order_item_moreinfo = ((LinearLayout) convertView.findViewById(R.id.order_item_moreinfo));
            viewHolder.order_item_number = ((TextView) convertView.findViewById(R.id.order_item_number));
            viewHolder.order_item_time = ((TextView) convertView.findViewById(R.id.order_item_time));
            viewHolder.order_item_people = ((TextView) convertView.findViewById(R.id.order_item_people));
            viewHolder.order_item_telpheone = ((TextView) convertView.findViewById(R.id.order_item_telpheone));
            viewHolder.order_item_address = ((TextView) convertView.findViewById(R.id.order_item_address));
            viewHolder.order_item_lefttop = ((TextView) convertView.findViewById(R.id.order_item_lefttop));
            viewHolder.order_item_contactservice = ((TextView) convertView.findViewById(R.id.order_item_contactservice));
            viewHolder.order_item_look_send = ((TextView) convertView.findViewById(R.id.order_item_look_send));
            viewHolder.order_item_pic = ((SimpleDraweeView) convertView.findViewById(R.id.order_item_pic));
            viewHolder.order_shop_name = ((TextView) convertView.findViewById(R.id.order_shop_name));
            viewHolder.order_shop_price = ((TextView) convertView.findViewById(R.id.order_shop_price));
            viewHolder.order_shopsnum = ((TextView) convertView.findViewById(R.id.order_shopsnum));
            viewHolder.rl_service = ((RelativeLayout) convertView.findViewById(R.id.rl_service));
            viewHolder.order_item_again_send = ((TextView) convertView.findViewById(R.id.order_item_again_send));
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (ynOrderList.get(groupPosition).getItem().size() == 1) {
            viewHolder.rl_service.setVisibility(View.VISIBLE);
        } else {
            viewHolder.rl_service.setVisibility(View.GONE);
        }

        viewHolder.order_item_lefttop.setText("订单状态："+ynOrderList.get(groupPosition).getEbOrderStatus());
        if (ynOrderList.get(groupPosition).getEbOrderStatus().equals("未支付")) {
            viewHolder.order_item_look_send.setVisibility(View.GONE);
            viewHolder.order_item_again_send.setVisibility(View.VISIBLE);
        } else {
            viewHolder.order_item_look_send.setVisibility(View.VISIBLE);
            viewHolder.order_item_again_send.setVisibility(View.GONE);
        }
        viewHolder.order_item_number.setText(ynOrderList.get(groupPosition).getEbOrderNumber());
        viewHolder.order_item_time.setText(ynOrderList.get(groupPosition).getEbOrderPlaceTime());
        viewHolder.order_item_people.setText(ynOrderList.get(groupPosition).getAddressReceiver());
        viewHolder.order_item_telpheone.setText(ynOrderList.get(groupPosition).getAddressPhoneNumber());
        viewHolder.order_item_address.setText(ynOrderList.get(groupPosition).getAddressDetail());
        ImageOptHelper.showFrescoTodayshow(Uri.parse(ynOrderList.get(groupPosition).getItem().get(0).getEbGoodsPic()), viewHolder.order_item_pic, context);
        viewHolder.order_shop_name.setText(ynOrderList.get(groupPosition).getItem().get(0).getEbGoodsName());
        viewHolder.order_shop_price.setText("¥" + ynOrderList.get(groupPosition).getItem().get(0).getEbGoodsPrice());
        viewHolder.order_shopsnum.setText("数量：" + ynOrderList.get(groupPosition).getItem().get(0).getItemNum());
        final ViewHolder finalViewHolder = viewHolder;
        viewHolder.order_item_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!ynOrderList.get(groupPosition).isShow()) {
                    ynOrderList.get(groupPosition).setShow(true);
                    finalViewHolder.order_item_moreinfo.setVisibility(View.VISIBLE);
                    finalViewHolder.order_item_show.setImageResource(R.drawable.order_item_showno);
                } else {
                    ynOrderList.get(groupPosition).setShow(false);
                    finalViewHolder.order_item_moreinfo.setVisibility(View.GONE);
                    finalViewHolder.order_item_show.setImageResource(R.drawable.order_item_showyes);
                }
            }
        });

        viewHolder.order_item_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ynOrderList.get(groupPosition).getItem().get(0).getEbGoodsID() == 1) {

                    context.startActivity(new Intent(context, VoiceCardPreViewActivity.class).putExtra("itemID", ynOrderList.get(groupPosition).getItem().get(0).getItemID()));

                } else if (ynOrderList.get(groupPosition).getItem().get(0).getEbGoodsID() == 2) {

                    context.startActivity(new Intent(context, TimeGoodsLomoPreViewActivity.class).putExtra("itemID", ynOrderList.get(groupPosition).getItem().get(0).getItemID()));

                } else if (ynOrderList.get(groupPosition).getItem().get(0).getEbGoodsID() == 3) {

                    context.startActivity(new Intent(context, TimeGoodsJointActivity.class).putExtra("itemID", ynOrderList.get(groupPosition).getItem().get(0).getItemID()));

                } else if (ynOrderList.get(groupPosition).getItem().get(0).getEbGoodsID() == 4) {

                    context.startActivity(new Intent(context, TimeGoodsDrawActivity.class).putExtra("itemID", ynOrderList.get(groupPosition).getItem().get(0).getItemID()));

                }
            }
        });

//        final int ebGoodId = ynOrderList.get(groupPosition).getEbGoodsID();
        viewHolder.order_item_contactservice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, ShopContactServiceActivity.class));
            }
        });

        viewHolder.order_item_look_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LookSendWebActivity.class);
                intent.putExtra("ebOrderLogisticsCompany", ynOrderList.get(groupPosition).getEbOrderLogisticsCompany());
                intent.putExtra("ebOrderLogisticsNumber", ynOrderList.get(groupPosition).getEbOrderLogisticsNumber());
//                        Log.d("yuhao", "ebOrderLogisticsNumber="+listItems.getEbOrderLogisticsNumber());
//                        Log.d("yuhao", "ebOrderLogisticsCompany="+listItems.getEbOrderLogisticsCompany());
                if (ynOrderList.get(groupPosition).getEbOrderLogisticsCompany() == null || ynOrderList.get(groupPosition).getEbOrderLogisticsNumber() == null) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setMessage("商品正在制作中，请耐心等待！");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();

                } else {
                    context.startActivity(intent);
                }
            }
        });

        //去支付
        viewHolder.order_item_again_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShopCarListActivity.ynShopCarClearAll.clear();
                List<YNOrder.ItemBean> item = ynOrderList.get(groupPosition).getItem();
                double hejiPrice = item.get(0).getEbGoodsPrice();
                YNShopCar ynShopCar = new YNShopCar();
                ynShopCar.setEbGoodsName(item.get(0).getEbGoodsName());
                ynShopCar.setCartItemNum(item.get(0).getItemNum());
                ynShopCar.setEbGoodsID(item.get(0).getEbGoodsID());
                ynShopCar.setEbGoodsEnName(item.get(0).getEbGoodsEnName());
                ynShopCar.setEbGoodsPrice(item.get(0).getEbGoodsPrice() + "");
                ynShopCar.setCartItemID(Integer.parseInt(item.get(0).getItemID()));
                ynShopCar.setEbGoodsPic(item.get(0).getEbGoodsPic());
                ynShopCar.setEbGoodsUnit(item.get(0).getEbGoodsUnit());
                ynShopCar.setNowPrice(item.get(0).getEbGoodsPrice() * item.get(0).getItemNum() + "");
                ShopCarListActivity.ynShopCarClearAll.add(ynShopCar);
                context.startActivity(new Intent(context, ShopCarOrderActivity.class).putExtra("hejiPrice", hejiPrice + ""));
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ChildViewHolder();
            convertView = mInflater.inflate(R.layout.order_list_view, parent, false);
            viewHolder.order_item_pic = ((SimpleDraweeView) convertView.findViewById(R.id.order_item_pic));
            viewHolder.order_shop_name = ((TextView) convertView.findViewById(R.id.order_shop_name));
            viewHolder.order_shop_price = ((TextView) convertView.findViewById(R.id.order_shop_price));
            viewHolder.order_shopsnum = ((TextView) convertView.findViewById(R.id.order_shopsnum));
            viewHolder.shop_item = ((LinearLayout) convertView.findViewById(R.id.shop_item));
            viewHolder.order_item_contactservice = ((TextView) convertView.findViewById(R.id.order_item_contactservice));
            viewHolder.order_item_look_send = ((TextView) convertView.findViewById(R.id.order_item_look_send));
            viewHolder.rl_show = ((RelativeLayout) convertView.findViewById(R.id.rl_show));
            viewHolder.order_item_again_send = ((TextView) convertView.findViewById(R.id.order_item_again_send));
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }
        childPosition++;
        if (childPosition == getChildrenCount(groupPosition)) {
            viewHolder.rl_show.setVisibility(View.VISIBLE);
        } else {
            viewHolder.rl_show.setVisibility(View.GONE);
        }
        if (ynOrderList.get(groupPosition).getEbOrderStatus().equals("未支付")) {
            viewHolder.order_item_look_send.setVisibility(View.GONE);
            viewHolder.order_item_again_send.setVisibility(View.VISIBLE);
        } else {
            viewHolder.order_item_look_send.setVisibility(View.VISIBLE);
            viewHolder.order_item_again_send.setVisibility(View.GONE);
        }
        YNOrder datas = ynOrderList.get(groupPosition);
        ImageOptHelper.showImageLoaderLoMoPreView(datas.getItem().get(childPosition).getEbGoodsPic(), viewHolder.order_item_pic, context);
        viewHolder.order_shop_name.setText(datas.getItem().get(childPosition).getEbGoodsName());
        viewHolder.order_shop_price.setText("¥" + datas.getItem().get(childPosition).getEbGoodsPrice());
        viewHolder.order_shopsnum.setText("数量：" + datas.getItem().get(childPosition).getItemNum());

        viewHolder.order_item_contactservice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, ShopContactServiceActivity.class));
            }
        });

        viewHolder.order_item_look_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LookSendWebActivity.class);
                intent.putExtra("ebOrderLogisticsCompany", ynOrderList.get(groupPosition).getEbOrderLogisticsCompany());
                intent.putExtra("ebOrderLogisticsNumber", ynOrderList.get(groupPosition).getEbOrderLogisticsNumber());
//                        Log.d("yuhao", "ebOrderLogisticsNumber="+listItems.getEbOrderLogisticsNumber());
//                        Log.d("yuhao", "ebOrderLogisticsCompany="+listItems.getEbOrderLogisticsCompany());
                if (ynOrderList.get(groupPosition).getEbOrderLogisticsCompany() == null || ynOrderList.get(groupPosition).getEbOrderLogisticsNumber() == null) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setMessage("商品正在制作中，请耐心等待！");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();
                } else {
                    context.startActivity(intent);
                }
            }
        });

        final int finalChildPosition = childPosition;
        viewHolder.order_item_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ynOrderList.get(groupPosition).getItem().get(finalChildPosition).getEbGoodsID() == 1) {
                    context.startActivity(new Intent(context, VoiceCardPreViewActivity.class).putExtra("itemID", ynOrderList.get(groupPosition).getItem().get(finalChildPosition).getItemID()));

                } else if (ynOrderList.get(groupPosition).getItem().get(finalChildPosition).getEbGoodsID() == 2) {

                    context.startActivity(new Intent(context, TimeGoodsLomoPreViewActivity.class).putExtra("itemID", ynOrderList.get(groupPosition).getItem().get(finalChildPosition).getItemID()));

                } else if (ynOrderList.get(groupPosition).getItem().get(finalChildPosition).getEbGoodsID() == 3) {

                    context.startActivity(new Intent(context, TimeGoodsJointActivity.class).putExtra("itemID", ynOrderList.get(groupPosition).getItem().get(finalChildPosition).getItemID()));

                } else if (ynOrderList.get(groupPosition).getItem().get(finalChildPosition).getEbGoodsID() == 4) {

                    context.startActivity(new Intent(context, TimeGoodsDrawActivity.class).putExtra("itemID", ynOrderList.get(groupPosition).getItem().get(finalChildPosition).getItemID()));

                }
            }
        });
        //去支付
        viewHolder.order_item_again_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShopCarListActivity.ynShopCarClearAll.clear();
                for (int i = 0; i < ynOrderList.get(groupPosition).getItem().size(); i++) {
                    List<YNOrder.ItemBean> item = ynOrderList.get(groupPosition).getItem();
                    double hejiPrice = item.get(i).getEbGoodsPrice() * item.get(i).getItemNum();
                    YNShopCar ynShopCar = new YNShopCar();
                    ynShopCar.setEbGoodsName(item.get(i).getEbGoodsName());
                    ynShopCar.setCartItemNum(item.get(i).getItemNum());
                    ynShopCar.setEbGoodsID(item.get(i).getEbGoodsID());
                    ynShopCar.setEbGoodsEnName(item.get(i).getEbGoodsEnName());
                    ynShopCar.setEbGoodsPrice(item.get(i).getEbGoodsPrice() + "");
                    ynShopCar.setCartItemID(Integer.parseInt(item.get(i).getItemID()));
                    ynShopCar.setEbGoodsPic(item.get(i).getEbGoodsPic());
                    ynShopCar.setEbGoodsUnit(item.get(i).getEbGoodsUnit());
                    ynShopCar.setNowPrice(item.get(i).getEbGoodsPrice() * item.get(i).getItemNum() + "");
                    totalPrice = hejiPrice + totalPrice;
//                    Log.d("yuhao", "itemId="+i);
                    ShopCarListActivity.ynShopCarClearAll.add(ynShopCar);
                }
                context.startActivity(new Intent(context, ShopCarOrderActivity.class).putExtra("hejiPrice", totalPrice + ""));
                totalPrice = 0.0;
            }
        });
        return convertView;
    }
    double totalPrice = 0.0;

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public class ViewHolder {
        ImageView order_item_show;
        LinearLayout order_item_moreinfo;
        TextView order_item_number;
        TextView order_item_time;
        TextView order_item_people;
        TextView order_item_telpheone;
        TextView order_item_address;
        TextView order_item_lefttop;
        TextView order_item_contactservice;
        TextView order_item_look_send;
        SimpleDraweeView order_item_pic;
        TextView order_shop_name;
        TextView order_shop_price;
        TextView order_shopsnum;
        RelativeLayout rl_service;
        TextView order_item_again_send;
    }

    public class ChildViewHolder {
        SimpleDraweeView order_item_pic;
        TextView order_shop_name;
        TextView order_shop_price;
        TextView order_shopsnum;
        LinearLayout shop_item;
        TextView order_item_contactservice;
        TextView order_item_look_send;
        RelativeLayout rl_show;
        TextView order_item_again_send;
    }
}
